package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.entity.VideoLiveRecord;
import com.wyh.mapper.VideoLiveRecordMapper;
import com.wyh.request.VideoLiveRecordRequest;
import com.wyh.service.IVideoLiveRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 直播记录管理业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class VideoLiveRecordServiceImpl implements IVideoLiveRecordService {

    private final VideoLiveRecordMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VideoLiveRecord add(VideoLiveRecordRequest request) {
        VideoLiveRecord entity = VideoLiveRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(VideoLiveRecordRequest request) {
        VideoLiveRecord entity = VideoLiveRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public VideoLiveRecord getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<VideoLiveRecord> pageList(VideoLiveRecordRequest request) {
        Page<VideoLiveRecord> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<VideoLiveRecord> query = this.buildWrapper(request);
        IPage<VideoLiveRecord> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<VideoLiveRecord> list(VideoLiveRecordRequest request) {
        LambdaQueryWrapper<VideoLiveRecord> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<VideoLiveRecord> buildWrapper(VideoLiveRecordRequest param) {
        LambdaQueryWrapper<VideoLiveRecord> query = new LambdaQueryWrapper<>();
        if (param.getLiveRoomId() != null){
               query.eq(VideoLiveRecord::getLiveRoomId ,param.getLiveRoomId());
        }
        if (param.getMasterId() != null){
               query.eq(VideoLiveRecord::getMasterId ,param.getMasterId());
        }
         if (StringUtils.isNotBlank(param.getLiveTitle())){
               query.like(VideoLiveRecord::getLiveTitle ,param.getLiveTitle());
        }
         if (StringUtils.isNotBlank(param.getLiveDescription())){
               query.like(VideoLiveRecord::getLiveDescription ,param.getLiveDescription());
        }
        if (param.getLiveStatus() != null){
               query.eq(VideoLiveRecord::getLiveStatus ,param.getLiveStatus());
        }
         if (StringUtils.isNotBlank(param.getLiveAddress())){
               query.like(VideoLiveRecord::getLiveAddress ,param.getLiveAddress());
        }
        if (param.getIsCharge() != null){
               query.eq(VideoLiveRecord::getIsCharge ,param.getIsCharge());
        }
        if (param.getLiveTime() != null){
               query.eq(VideoLiveRecord::getLiveTime ,param.getLiveTime());
        }
        if (param.getLiveGiftNum() != null){
               query.eq(VideoLiveRecord::getLiveGiftNum ,param.getLiveGiftNum());
        }
        if (param.getLivePersonNum() != null){
               query.eq(VideoLiveRecord::getLivePersonNum ,param.getLivePersonNum());
        }
        if (param.getNewFanNum() != null){
               query.eq(VideoLiveRecord::getNewFanNum ,param.getNewFanNum());
        }
        if (param.getPlaybackId() != null){
               query.eq(VideoLiveRecord::getPlaybackId ,param.getPlaybackId());
        }
        if (param.getIsDelete() != null){
               query.eq(VideoLiveRecord::getIsDelete ,param.getIsDelete());
        }
        if (param.getCreateUserId() != null){
               query.eq(VideoLiveRecord::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(VideoLiveRecord::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
